import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_juwo/common/common_index.dart';

class VerificationCodeView extends StatefulWidget {
  int length;
  Function function;

  VerificationCodeView(int length, Function function) {
    this.length = length;
    this.function = function;
  }

  @override
  _VerificationCodeViewState createState() => _VerificationCodeViewState();
}

class _VerificationCodeViewState extends State<VerificationCodeView> {
  String result = "";
  List<Widget> widgets = [];
  List<FocusNode> focusNodes = [];
  List<TextEditingController> controllers = [];

  @override
  void initState() {
    super.initState();
    print("这么多个 ${widget.length}");
    for (int i = 0; i < widget.length; i++) {
      focusNodes.add(FocusNode());
      controllers.add(TextEditingController());
    }
    for (int i = 0; i < widget.length; i++) {
      widgets.add(getItem(i));
    }
    for (int i = 0; i < widget.length; i++) {
      focusNodes[i].addListener(() {
        print("${i}focusNodes--addListener");
      });

      focusNodes[i].removeListener(() {
        print("${i}focusNodes--removeListener");
      });

      controllers[i].removeListener(() {
        print("${i}controller--removeListener");
      });
      controllers[i].addListener(() {
        print("${i}controller--addListener");
      });
    }
  }

  Widget getItem(int index) {
    return Container(
      alignment: Alignment.center,
      height: ScreenUtil().setWidth(130),
      width: ScreenUtil().setWidth(130),
      decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(2),
          border: Border.all(
              width: ScreenUtil().setWidth(1), color: Color(0xFFe5e5e5))),
      child: TextField(
        onChanged: (String txt) {
          if (txt != "" && index < widget.length) {
            focusNodes[index].unfocus();
            FocusScope.of(context).requestFocus(focusNodes[index + 1]);
          } else if (index > 0) {
            focusNodes[index].unfocus();
            FocusScope.of(context).requestFocus(focusNodes[index - 1]);
          }
        },

        onSubmitted: (String txt) {},
        controller: controllers[index],

        focusNode: focusNodes[index],
        keyboardType: TextInputType.numberWithOptions(),
        textAlign: TextAlign.center,
        inputFormatters: [
          LengthLimitingTextInputFormatter(1),
          WhitelistingTextInputFormatter.digitsOnly
        ],
        autofocus: true,

        //是否自动对焦
        style: TextStyles.custom(48, Colours.appC1, Font.PingFangBlod),
        decoration: InputDecoration(
            hintText: "",
            hintStyle:
                TextStyles.custom(30, Colors.black, Font.PingFangBlod),
            border: InputBorder.none),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil().setWidth(90), 0, ScreenUtil().setWidth(90), 0),
      height: ScreenUtil().setWidth(200),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: widgets,
      ),
    );
  }
}
